<?php
/**
 * @link http://www.tintsoft.com/
 * @copyright Copyright (c) 2013 TintSoft LLC
 * @license http://www.tintsoft.com/license/
 */
if ( ! function_exists ( 'array_set' ) ) {
    /**
     * Set an array item to a given value using "dot" notation. If no key is
     * given to the method, the entire array will be replaced.
     *
     * @param array $array
     * @param string $key
     * @param mixed $value
     * @return void
     */
    function array_set(&$array, $key, $value)
    {
        if ( is_null ( $key ) ) return $array = $value;
        $keys = explode ( '.', $key );
        while ( count ( $keys ) > 1 ) {
            $key = array_shift ( $keys );
            if ( ! isset ( $array [$key] ) or ! is_array ( $array [$key] ) ) {
                $array [$key] = array ();
            }
            $array = & $array [$key];
        }
        $array [array_shift ( $keys )] = $value;
    }
}
if ( ! function_exists ( 'array_get' ) ) {
    /**
     * Get an item from an array using "dot" notation.
     *
     * @param array $array
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    function array_get($array, $key, $default = null)
    {
        if ( is_null ( $key ) ) return $array;
        if ( isset ( $array [$key] ) ) return $array [$key];
        foreach ( explode ( '.', $key ) as $segment ) {
            if ( ! is_array ( $array ) or ! array_key_exists ( $segment, $array ) ) {
                return value ( $default );
            }
            $array = $array [$segment];
        }
        return $array;
    }
}
if ( ! function_exists ( 'value' ) ) {
    /**
     * Return the default value of the given value.
     *
     * @param mixed $value
     * @return mixed
     */
    function value($value)
    {
        return $value instanceof Closure ? $value () : $value;
    }
}